package gov.nhso.prov.acc.apps.acc6;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.isud.core.ISUD;
import com.isud.core.ISUDFactory;
import com.isud.paging.PageBean;

import gov.nhso.prov.acc.beans.Acc6BaacHolidayBean;
import gov.nhso.prov.acc.core.DBConnection;
import gov.nhso.prov.acc.utils.Utility;

public class ACC60110 extends Utility {
	public String url = "/apps/acc6/ACC60110A.jsp";

	public ACC60110(HttpServletRequest request, HttpServletResponse response) {
		this.message(request).clearMsg();
		String job = this.getAttr(request, "job");
		request.setAttribute("iMonth", this.properties().iMonth());
		if (job.equals("init")) {
			this.init(request);
		} else if (job.equals("add")) {
			this.add(request);
		} else if (job.equals("save")) {
			this.save(request);
		} else if(job.equals("back")){
			if (this.getLastRequest(request, "lastSearch")) {
				this.search(request);
			} else {
				this.init(request);
			}
		} else if (job.equals("search")){
			this.setLastRequest(request, "lastSearch");
			this.search(request);
		} else if (job.equals("view")){
			this.view(request);
		} else if (job.equals("edit")) {
			this.edit(request);
		} else if (job.equals("del")) {
			this.del(request);
		}
	}
	
	private String init(HttpServletRequest request){
		return this.url;
	}
	
	private String add(HttpServletRequest request){
		request.setAttribute("job", "save");
		this.url = "/apps/acc6/ACC60110B.jsp";
		return this.url;
	}
	private String save(HttpServletRequest request){
 		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc6BaacHolidayBean bhb = new Acc6BaacHolidayBean();
			String ts = this.dateUtil().getCurrentTimeStamp2DB();
			this.beanUtil().copyRequest2Bean(request, bhb);
			bhb.setTimestamps(this.dateUtil().setTimeStamp2DB(ts));
			if (bhb.getYyyy() != null && !bhb.getYyyy().equals("")) {
				bhb.setYyyy(String.valueOf(Integer.parseInt(bhb.getYyyy()) - 543));
			}
			dao.insert(bhb);
			request.setAttribute("job", "view");
			request.setAttribute("timestamps", ts);
			this.message(request).setMsgCode("I002");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.view(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	private String view(HttpServletRequest request){
 		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc6BaacHolidayBean bhb = new Acc6BaacHolidayBean();
			String timestamps = this.getAttr(request, "timestamps");
			bhb.setTimestamps("TO_TIMESTAMP('" + timestamps + "', 'YYYYMMDDHH24MISSFF3')");
			Map<String, String> xcols = new HashMap<String, String>();
			xcols.put("TIMESTAMPS", "TO_CHAR(TIMESTAMPS, 'YYYYMMDDHH24MISSFF3')");
			dao.select(bhb, xcols, "");
			request.setAttribute("job", "edit");
			request.setAttribute("bhb", bhb);
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc6/ACC60110B.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String edit(HttpServletRequest request){
 		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc6BaacHolidayBean bhb = new Acc6BaacHolidayBean();
			this.beanUtil().copyRequest2Bean(request, bhb);
			String timestamps = bhb.getTimestamps();
			bhb.setTimestamps("TO_TIMESTAMP('" + bhb.getTimestamps() + "', 'YYYYMMDDHH24MISSFF3')");
			if (bhb.getYyyy() != null && !bhb.getYyyy().equals("")) {
				bhb.setYyyy(String.valueOf(Integer.parseInt(bhb.getYyyy()) - 543));
			}
			dao.update(bhb);
			request.setAttribute("timestamps", timestamps);
			this.message(request).setMsgCode("I002");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.view(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String search(HttpServletRequest request){
 		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			PageBean pageBean = new PageBean();
			Acc6BaacHolidayBean bhb = new Acc6BaacHolidayBean();
			this.beanUtil().copyRequest2Bean(request, bhb);
			this.beanUtil().copyRequest2Bean(request, pageBean);
			if (dao.chkData(pageBean.getOrderField()).equals("UPDATED_DATE")) {
				pageBean.setOrderField("MM");
				pageBean.setOrderType("ASC");
			}
			Map<String, String> xcols = new HashMap<String, String>();
			xcols.put("TIMESTAMPS", "TO_CHAR(TIMESTAMPS, 'YYYYMMDDHH24MISSFF3')");
			xcols.put("DD", "CAST (DD AS INT)");
			xcols.put("MM", "CAST (MM AS INT)");
			xcols.put("YYYY", "CAST (YYYY AS INT)");
			pageBean = dao.selectPage(bhb, xcols, "", pageBean);
			
			request.setAttribute("pageBean", pageBean);
			request.setAttribute("bhb", bhb);
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String del(HttpServletRequest request){
 		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc6BaacHolidayBean bhb = new Acc6BaacHolidayBean();
			String timestamps = this.getAttr(request, "timestamps");
			bhb.setTimestamps("TO_TIMESTAMP('" + timestamps + "', 'YYYYMMDDHH24MISSFF3')");
			dao.delete(bhb);
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				request.setAttribute("timestamps", "");
				this.search(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
}
